/*
 * @Autor: Shihh
 * @Description: 
 * @Version: 1.0
 * @Date: 2020-05-08 10:14:01
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2020-05-22 17:39:27
 */
const Sequelize = require('sequelize')
const Op = Sequelize.Op;
var comm = require('../comm')

module.exports = (sequelize, DataTypes) => {

    class m_Tag extends Sequelize.Model {

        /**
         * @author: Shihh
         * @description: 程序加载将tag存入redis
         * @param {type} 
         * @return: 
         */
        static async getAllTag() {
            let condition = {
                attributes: ['id', 'name'],
                raw: true,
            }
            let querydata = await this.findAll(condition)
            for (let item of querydata) {
                comm.redis.set(item.id, item.name, function (err, data) { })
            }
        }
        /**
         * @author: Shihh
         * @description: 分页获取所有标签
         * @param {type} 
         * @return: 
         */
        static async getTagList(pars) {
            var [page = 1, pagesize = 15, limit = 0] = [pars.page, pars.pagesize, pars.limit]
            let condition = {
                attributes: ['id', 'name', 'remark', 'group', 'group_name'],
                raw: true,
                offset: pagesize * (page - 1),
                limit: parseInt(pagesize),
                where:pars.where
            }
            return await this.findAndCountAll(condition)
             ;
        }
        /**
         * @author: Shihh
         * @description: 条件获取单条标签
         * @param {type} 
         * @return: 
         */
        static async getOneTag(paras) {
            let where = {}
            let condition = {
                attributes: ['id', 'name', 'remark', 'group', 'group_name'],
            }
            if (paras.id != undefined && paras.id != null) {
                where.id = paras.id
            }
            if (paras.name != undefined && paras.name != null) {
                where.name = paras.name
            }
            condition.where = where
            let data = await this.findOne(condition)
            return data
        }
        /**
         * @author: Shihh
         * @description: 获取标签总数
         * @param {type} 
         * @return: 
         */
        static getTagCount() {
            return this.count()
        }

        /**
         * @author: Shihh
         * @description: 更新标签
         * @param {type} 
         * @return: 
         */
        static async updateTag(paras) {
            return await this.update({
                name: paras.name,
                group: paras.group,
                group_name: paras.group_name,
                remark: paras.remark
            }, {
                'where': {
                    'id': paras.id
                }
            }).then(re => {
                comm.redis.set(paras.id, paras.name)
                return true
            }).catch(err => {
                console.log(err)
                return false
            })
        }
        /**
         * @author: Shihh
         * @description: 新增标签
         * @param {type} 
         * @return: 
         */
        static async saveTag(paras) {
            return await this.create({
                name: paras.name,
                group: paras.group,
                group_name: paras.group_name,
                remark: paras.remark
            }
            ).then(re => {
                return true
            }).catch(err => {
                return false
            })
        }

        static async getTagByName(name){
            let tagIds=await this.findAll({where:{name: { [Op.like]: '%' + name + '%'}},raw:true})
            return tagIds
        }

    }

    m_Tag.init({
        id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
        name: { type: DataTypes.STRING(64) },
        remark: { type: DataTypes.STRING(255) },
        group: { type: DataTypes.INTEGER },
        group_name: { type: DataTypes.STRING(45) },
    }, {
        sequelize,
        modelName: 'tags',
        freezeTableName: true,
        timestamps: false
    });

    return m_Tag;

}

